package com.sp.pay.action.company;

import com.sp.pay.common.CacheUtil;
import com.sp.pay.dto.QueryDto;
import com.sp.pay.dto.company.CompanyDto;
import com.sp.pay.dto.company.CompanyParameterDto;
import com.sp.pay.repository.company.CompanyRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "company/")
public class CompayAction {

    private  String INDEX = "company/index";
    private  String INDEXPARA = "company/indexPara";
    private  String ADD = "company/add";
    private  String EDIT = "company/edit";

    private static Log logger = LogFactory.getLog(CompayAction.class);

    @Autowired
    CompanyRepository companyRepository;

    @RequestMapping(value ="index.do" )
    public String index(Model model,String company_type){
    	CompanyDto companyDto = new CompanyDto();
    	companyDto.setCompany_type(company_type);
        QueryDto<CompanyDto> queryDto = new QueryDto<CompanyDto>(companyDto,0,100);
        model.addAttribute("result",companyRepository.query(queryDto));
        model.addAttribute("company_type", company_type);
        return INDEX;
    }

    @RequestMapping(value ="{company_type}/newCompany.do" )
    public String newUser(@PathVariable("company_type") String company_type,Model model){
    	model.addAttribute("company_type", company_type);
        return ADD;
    }

    @RequestMapping(value ="editCompany.do" )
    public String editUser(Model model, int id){
    	CompanyDto companyDto = companyRepository.findById(id);
        model.addAttribute("company",companyDto);
        model.addAttribute("company_type", companyDto.getCompany_type());
        return EDIT;
    }

    @RequestMapping(value ="delCompany.do" )
    public String delUser(Model model,@PathVariable("id") int id,String company_type){
        companyRepository.delete(id);
        model.addAttribute("company_type", company_type);
        return index(model,company_type);
    }

    @RequestMapping(value ="save.do" )
    public String save(Model model,CompanyDto companyDto){
        companyRepository.save(companyDto);
        CacheUtil.putCompany(companyDto.getCompany_num()+"_"+companyDto.getCompany_type(),companyDto);
        return index(model,companyDto.getCompany_type());

    }

    @RequestMapping(value ="{companyType}/getCompany.do" )
    @ResponseBody
    public CompanyDto getCompany(@PathVariable("companyType")String companyType){
        CompanyDto companyDto = new CompanyDto();
        companyDto.setCompany_type(companyType);
        QueryDto<CompanyDto> queryDto = new QueryDto<CompanyDto>(companyDto,0,1000);
        return queryDto.getDto();
    }

    @RequestMapping(value ="indexPara/{company_num}.do" )
    public String indexPara(@PathVariable("company_num") String company_num, Model model){
        CompanyParameterDto sDto = companyRepository.queryS_Para(company_num);
        if(sDto==null || sDto.getId()==0){
            sDto = new CompanyParameterDto();
        }
        CompanyParameterDto pDto = companyRepository.queryP_Para(company_num);
        if(pDto==null || pDto.getId()==0){
            pDto = new CompanyParameterDto();
        }
        model.addAttribute("s_para",sDto);
        model.addAttribute("p_para",pDto);
        return INDEXPARA;
    }

    @RequestMapping(value ="savePara.do" )
    @ResponseBody
    public String savePara(Model model,CompanyParameterDto companyParameterDto){
        companyRepository.savePara(companyParameterDto);
        CacheUtil.putCompanyPara(companyParameterDto.getCompany_num()+"_"+companyParameterDto.getParameter_type(),companyParameterDto);
        return "ok";

    }
}
